﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMP_QuanLySV.DAL;
using SMP_QuanLySV.BLL;

namespace SMP_QuanLySV.Presentation
{
    public partial class UC_BaoLuu : UserControl
    { 
        Form parent;
        BLLBaoLuu BLL = new BLLBaoLuu();
        int selectedIndex = -1;
        BaoLuu selectedInstance = null;
        string trangThai = "";

        // constructor
        public UC_BaoLuu(Form parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        void setDefaultSelectedInstance()
        {
            selectedIndex = 0;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            // update du lieu cho phu hop voi selected row
            updateTextboxes(selectedInstance);
        }

        // update cac textboxes cho phu hop voi selected row trong datagridview
        void updateTextboxes(BaoLuu temp)
        {
            txtMaSv.Text = temp.SVID;
            txtSoQD.Text = temp.SoQDBL.ToString();
            txtThoiHanBL.Text = temp.ThoiHanBL;
            txtNgayBL.Text= temp.NgayBL;
            txtNoiDung.Text =temp.NoiDungBL;
        }

        // set thuoc tinh readOnly/enabled cho tat ca cac controls ve 1 gia tri
        void setReadOnlyAll(bool val)
        {
            txtMaSv.ReadOnly = val;
            txtSoQD.ReadOnly= val;
            txtThoiHanBL.ReadOnly = val;
            txtNoiDung.ReadOnly = val;
            txtNgayBL.ReadOnly = val;
        }

        // cho thuoc tinh text cua cac textboxes tro thanh ""
        private void clearTextboxes()
        {
            txtMaSv.Text = "";
            txtNoiDung.Text= "";
            txtThoiHanBL.Text = "";
            txtSoQD.Text = "";
            txtNgayBL.Text = "";
        }

        // xy ly su kien click chon 1 cell khac trong datagridview
        // --> lay row cua cell do lam selectedIndex va SelectedInstance bang row dc chon
        private void dataGridViewX2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedIndex = dataGridViewX2.SelectedCells[0].RowIndex;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            updateTextboxes(selectedInstance);
        }

        void setBtnEnabled1(bool val)
        {
            btnThem.Enabled = val;
            btnXoa.Enabled = val;
            btnSua.Enabled = val;
        }

        void setBtnEnabled2(bool val)
        {
            btnLuu.Enabled = val;
            btnHuy.Enabled = val;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            clearTextboxes();
            setBtnEnabled2(false);
            setBtnEnabled1(false);
            txtMaSv.ReadOnly = false;
            btnTimKiem.Enabled = true;

            // set trang thai ve che do them moi
            trangThai = "dang_them";
        
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            if (txtMaSv.Text == "")
            {
                MessageBox.Show("Bạn phải chọn sinh viên bảo lưu nào cần xóa", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa sinh viên bảo lưu này không?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.Yes)
                {
                    // xoa doi tuong va update lai view
                    string BaoLuuIDCanDelete = txtMaSv.Text;
                    BaoLuu BaoLuuCanDelete = BLL.retrieveByID(BaoLuuIDCanDelete);
                    BLL.deleteBaoLuu(BaoLuuCanDelete);

                    dataGridViewX2.DataSource = BLL.returnListBaoLuu();
                    setDefaultSelectedInstance();
                }

            }
            btnTimKiem.Enabled = false;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            setReadOnlyAll(false);
            txtMaSv.ReadOnly = true;

            // set trang thai ve che do sua
            trangThai = "dang_sua";
            setBtnEnabled1(false);
            setBtnEnabled2(true);
            btnTimKiem.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            // disable toan bo cac textboxes
            setReadOnlyAll(true);

            // kiem tra tinh hop le cua du lieu nhap vao
            BaoLuu tg = new BaoLuu();
            if (txtSoQD.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập thông tin SoQĐ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }
            else
            {
                tg.SVID = txtMaSv.Text;
                tg.SoQDBL = int.Parse(txtSoQD.Text);
                tg.ThoiHanBL = txtThoiHanBL.Text;
                tg.NoiDungBL = txtNoiDung.Text;
                tg.NgayBL = txtNgayBL.Text;
            }

            if (BLL.kiemTra(tg))
            {
                // update csdl va view
                if (trangThai == "dang_sua")
                {
                    BLL.updateBaoLuu(tg);
                }
                if (trangThai == "dang_them")
                {
                    BLL.insertBaoLuu(tg);
                }
                dataGridViewX2.DataSource = BLL.returnListBaoLuu();
                // disable cac nut Luu va Huy
                setBtnEnabled2(false);
                setBtnEnabled1(true);
                setDefaultSelectedInstance();
            }
            else
            {
                MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }
            btnTimKiem.Enabled = false;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            // update lai view va buttons
            setDefaultSelectedInstance();
            setReadOnlyAll(true);
            dataGridViewX2.DataSource = BLL.returnListBaoLuu();
            setBtnEnabled2(false);
            setBtnEnabled1(true);
            btnTimKiem.Enabled = false;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            setBtnEnabled2(true);
            if (txtMaSv.Text == "")
            {
                MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }
            else
            {
                dataGridViewX1.DataSource = BLL.BindingSV(txtMaSv.Text);
                setReadOnlyAll(false);
            }
        }

        private void UC_BaoLuu_Load(object sender, EventArgs e)
        {
            // nap du lieu cho datagridview
            dataGridViewX2.DataSource = BLL.returnListBaoLuu();

            // mac dinh dong dau tien trong datagridview duoc chon (selected)
            setDefaultSelectedInstance();
            setReadOnlyAll(true);
        }
    }
}
